<template>
    <div class="f-menu" :style="{ width: useLayout.asideWidth }">
        <el-menu default-active="defaultActive" class="border-0" @select="handleSelect" :collapse="useLayout.isCollapse"
            :collapse-transition="false" :unique-opened="true">
            <template v-for="(item, index) in asideMenu" :key="index">
                <el-sub-menu v-if="item.child && item.child.length > 0" :key="item" :index="item.name">
                    <template #title>
                        <el-icon>
                            <component :is="item.icon"></component>
                        </el-icon>
                        <span>{{ item.name }}</span>
                    </template>
                    <el-menu-item v-for="(item2, index2) in item.child" :key="index2" :index="item2.frontpath">
                        <el-icon>
                            <component :is="item2.icon"></component>
                        </el-icon>
                        <span>{{ item2.name }}</span>
                    </el-menu-item>
                </el-sub-menu>
                <el-menu-item v-else :index="item.frontpath">
                    <el-icon>
                        <component :is="item.icon"></component>
                    </el-icon>
                    <span>{{ item.name }}</span>
                </el-menu-item>
            </template>
        </el-menu>
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useLayoutStore } from '@/stores/index'
const useLayout = useLayoutStore();
const router = useRouter();
const route = useRoute();

// 默认选中
const defaultActive = ref(route.path)

const asideMenu = [{
    "name": "后台面板",
    "icon": "help",
    "child": [{
        "name": "首页",
        "icon": "house",
        "frontpath": "/"
    }]
}, {
    "name": "商城管理",
    "icon": "shopping-bag",
    "child": [{
        "name": "商品管理",
        "icon": "shopping-cart-full",
        "frontpath": "/goods/list"
    }]
}]


const handleSelect = (key) => {
    console.log(key)
    router.push(key)
}

</script>

<style scoped>
.f-menu {
    transition: 0.2s;
    width: 250px;
    top: 64px;
    bottom: 0;
    left: 0;
    overflow-y: auto;
    overflow-x: hidden;
    @apply shadow-md fixed bg-light-50;
}
</style>